Skip to content

feat(ai): create, list and delete alerts from the chat (create_alert, list_alerts, delete_alert) - #10233

Open
nishantmonu51 wants to merge 6 commits into
nishant/alerts-reports-for-userfrom
nishant/chat-alerts
Open

nishantmonu51 wants to merge 6 commits into
nishant/alerts-reports-for-userfrom
nishant/chat-alerts

Conversation

@nishantmonu51

Copy link
Copy Markdown
Collaborator

Takes over #10122 with the create-and-delete-only direction from its review. Stacked on #10232.

  • create_alert creates a formula alert, saving the dialog's MetricsViewAggregation query with its sort and comparison measures, or an AI alert.
    • It requires an explore name and writes comparisons as Rill time offsets.
    • It checks AI conditions against the parser's schedule rules before saving.
    • There's no editing: to change an alert, the analyst links to its page.
  • list_alerts reads the user's own alerts from the runtime catalog: name, display name, explore, a one-line condition and the URL of the alert's page.
  • delete_alert only deletes the user's own alerts. Its first call returns confirmation_required, and a confirmed call only deletes if the user sent a message since. A prompt injection in tool results can't confirm on the user's behalf.
  • Recipients are limited to project members by the admin service (previous PR), which covers the exfiltration concern for creates.
  • The tools are offered to users in prod deployments, not to services or to AI report and alert sessions. They're not exposed on the org-level MCP server, which has no conversation to confirm in.
  • The tests create, list and delete alerts through the real admin service and runtime.

Open for review: the delete gate is weaker than a click. A confirm button in the web chat would need a chat card plus a runtime call; that would be a follow-up if we want it.

Checklist:

  • Covered by tests
  • Ran it and it works as intended
  • Reviewed the diff before requesting a review
  • Checked for unhandled edge cases
  • Linked the issues it closes
  • Checked if the docs need to be updated. If so, create a separate Linear DOCS issue
  • Intend to cherry-pick into the release branch
  • I'm proud of this work!

…`, `list_alerts`, `delete_alert`)

Adds tools that let the analyst create alerts for the user, list the user's own alerts and delete them,
through the admin service's create- and delete-for-user API. Following the review of #10122, the chat only creates and deletes:
to change an alert, the analyst links to its page, where the user edits it in the dialog.

- `create_alert` saves what the alert dialog saves: a formula alert over the dialog's `MetricsViewAggregation` query (with its sort and comparison measures),
  or an AI alert whose condition the `ai` resolver checks. It requires an explore, writes comparisons as Rill time offsets,
  and checks AI alerts against the parser's schedule rules (a cron of at most once an hour) before saving.
- What it creates can only reach project members: the admin service defaults recipients to the user and rejects others.
- `list_alerts` reads the user's alerts from the runtime catalog: name, display name, explore, a one-line condition and the URL of the alert's page.
- `delete_alert` only deletes the user's own alerts, and only once the user confirmed in a message after the tool asked for it,
  so what the tools read can't stand in for the user's confirmation.
- The tools are only offered to users in prod deployments, not to services or to the sessions of AI reports and alerts.
- Tests create alerts through the real admin service, so the YAML, parser and reconcile path is covered.

Based on #10122.

Co-authored-by: dfliess <dfliess@gmail.com>

@nishantmonu51 nishantmonu51 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things inline about creating and editing alerts.

Comment thread runtime/ai/alert_create.go Outdated
opts := &adminv1.AlertOptions{
DisplayName: args.DisplayName,
MetricsViewName: explore.MetricsView,
WebOpenPath: "/explore/" + args.Explore,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An Explore named Revenue #1 is saved as /explore/Revenue #1, so getExploreName reads only Revenue and the criteria alert page can't find its Explore. Could we use url.PathEscape(args.Explore) here and decode the segment in ListAlerts.Handler? AlertForm.svelte already escapes this when creating an alert.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8fee0ab: the open path uses url.PathEscape(args.Explore) and list_alerts decodes it, tested with an explore named Revenue #1.

} else if args.Comparison != "" {
return errors.New("invalid input: comparison only applies to criteria of type 'absolute_change' or 'percent_change'")
}
if percentOfTotal {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A percent_of_total criterion without dimension compares the overall total with itself, so a condition like “any country is under 20% of revenue” can save an alert that never fires. Can setCriteria require a split dimension for percent-of-total, and criteriaHaving reject thresholds outside 0–100, matching the checks in form-utils.ts?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8fee0ab: percent-of-total criteria now need a dimension and a threshold from 0 to 100.

// The tools only create and delete: to change an alert or report, the user edits it in its dialog.

// confirmBeforeCreating is the part of the create tools' descriptions that makes the model confirm with the user.
const confirmBeforeCreating = "Before calling this tool, show the user a short preview of what you will create (name, data, condition or content, schedule and recipients), then end your turn and wait for them to confirm. " +

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this preview a required step in the handler? If the analyst skips it, CreateAlert.Handler can schedule emails or Slack DMs to project members immediately. A confirmation action from the chat UI could commit the exact draft the user saw, including recipients and schedule. create_report could use the same flow.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as is here: the earlier review picked limiting recipients to project members as the check enforced in code for creates, and a required preview with a chat UI confirm across both create tools is a larger change I have flagged as a follow-up decision.

Comment thread runtime/ai/alert_report.go Outdated
}
root = parent
}
if root.ID != current {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A delete proposal stays eligible after the user declines it or moves on, since proposedInEarlierTurn accepts any older matching result. Could we expire it after the next user turn and require a one-time user confirmation tied to that alert before calling del?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partly fixed in 8fee0ab: a proposal now only counts in the turn right after it, so it expires once the user moves on; a click-to-confirm in the chat UI is the follow-up noted in the PR description.

Comment thread runtime/ai/alert_report.go Outdated

// Schedule is when a report is sent or an alert is checked.
type Schedule struct {
Cron string `json:"cron" jsonschema:"Standard 5-field cron expression, for example '0 9 * * 1' for Mondays at 9:00."`

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked 0 9 15 * * against the dialog's schedule helpers. Chat can save the 15th, then getExistingScheduleFormValues shows the 1st and saving a rename moves the schedule. Could ScheduleForm.svelte and its save handlers preserve the original cron when the schedule is untouched, and show the actual schedule for values the picker can't edit? This applies to chat-created reports too.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed on the tool side in 8fee0ab: the shared checkSchedule only accepts the daily, weekdays, weekly and monthly-on-the-1st crons the dialog can edit, so 0 9 15 * * and hourly crons are rejected (this covers create_report once #10234 picks it up); keeping other crons in ScheduleForm.svelte is a dialog change left for a follow-up.

Comment thread runtime/ai/alert_list.go
if err := protojson.Unmarshal([]byte(argsJSON), q); err != nil || q.Having == nil {
return ""
}
sql, err := metricsview.ExpressionToSQL(metricsview.NewExpressionFromProto(q.Having))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list_alerts returns the same revenue_delta_perc < -0.1 for a week-over-week drop and a year-over-year drop. If a user has both, the analyst can't tell which one they mean from the condition. Could alertCondition include q.ComparisonTimeRange.Expression when present?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8fee0ab: the condition now ends with , compared with <comparison time range> when there is one.

Comment thread runtime/ai/alert_create.go Outdated
Exprs: []*runtimev1.Expression{expressionpb.Identifier(name), expressionpb.Number(value)},
}}})
}
return &runtimev1.Expression{Expression: &runtimev1.Expression_Cond{Cond: &runtimev1.Condition{Op: op, Exprs: exprs}}}, comparison, percentOfTotal, nil

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could criteriaHaving pick expressionpb.And or expressionpb.Or in the joiner switch and call that helper here? Both construct the exact same protobuf wrapper.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8fee0ab: the joiner switch picks expressionpb.And or expressionpb.Or.

- Escape the explore name in the alert's open path, and decode it in list_alerts.
- Require a dimension for percent-of-total criteria and a threshold from 0 to 100, as the dialog does.
- Only accept schedules the dialogs can edit, so saving an edit there doesn't move them.
- A delete proposal expires after the user's next message.
- list_alerts shows the time range a change is compared with.
- Join criteria with expressionpb.And and expressionpb.Or.
@pjain1

pjain1 commented Nov 29, 2026

Copy link
Copy Markdown
Member

The tools are much smaller and the saved query matches the dialog's, including the sort and the comparison measures. Accepting only the crons the dialog can show is the right call, so that editing in the UI keeps working. Three changes.

Explore names. I asked for the metrics-view-name fallback to be dropped in the first review, and that was a mistake. The analyst has no tool that lists explores, so in a project-level chat the model only knows metrics view names, and validExplore then fails with a "not found" error that gives it nothing to retry with. Please make that error list the explores the user can access. That is enough for the model to correct itself, and it avoids bringing the fallback back.

Delete confirmation. proposedInPreviousTurn walks the session's message tree to prove that the user replied between two delete calls. It is the most intricate code in the stack, and what it protects is one of the user's own alerts, which they can recreate. I asked for enforcement here, but for that risk the tool description is enough, as it is for create. Please drop the turn check and the two-call protocol.

Slack channels. With the change requested on #10232, please add slack_channels to Recipients.

Smaller point. The parser.ValidateAIAlertSchedule call in setAICondition is redundant, since dialogCron already limits schedules to daily or less often. The call and the export can go; the "an AI alert needs a schedule" check stays.


Developed in collaboration with Claude Code

…e delete gate after review

- validExplore's not found error lists the explores the user can access, with their metrics views, so the analyst can retry when it only knows a metrics view's name.
- delete_alert deletes in one call: its description has the model confirm with the user first, as for create_alert. proposedInPreviousTurn and the confirmed argument are gone.
- Recipients has slack_channels, and create_alert rejects Slack recipients when the project has no Slack connector, as the dialog does.
- setAICondition no longer calls the parser's schedule check, which dialogCron already covers, so the parser's function is unexported again.
- create_alert builds the alert's URL with resourcePageURL, since the admin service no longer returns it.
@nishantmonu51

Copy link
Copy Markdown
Collaborator Author

@pjain1 Thanks, done in 241a977 (after merging #10232 in 2b236dc):

  • Explore names: when the explore isn't one the user can access, the error now lists those explores with their metrics views, for example e1 (metrics view mv1); no fallback.
  • Delete confirmation: proposedInPreviousTurn and the confirmed argument are gone, so delete_alert deletes in one call and its description has the model confirm first, as for create.
  • Slack channels: Recipients has slack_channels, and create_alert rejects Slack recipients when the project has no Slack connector, as the dialog does.
  • Smaller point: dropped the ValidateAIAlertSchedule call and the export; the schedule-required check stays.

@nishantmonu51 nishantmonu51 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things inline.

Comment thread runtime/ai/alert_report.go Outdated
// The explore exists, so the error is about something else, like an invalid spec
return nil, err
}
accessible = append(accessible, fmt.Sprintf("%s (metrics view %s)", r.Meta.Name.Name, r.GetExplore().Spec.MetricsView))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a bad Explore edit is staged, the dashboard can still serve its last valid spec. This fallback reads the draft Spec, so the chat can suggest an Explore with the wrong metrics view or one it can't create an alert for. Can we build the list from ValidSpec, the same spec the create path uses?

Suggested change
accessible = append(accessible, fmt.Sprintf("%s (metrics view %s)", r.Meta.Name.Name, r.GetExplore().Spec.MetricsView))
if spec := r.GetExplore().GetState().GetValidSpec(); spec != nil {
accessible = append(accessible, fmt.Sprintf("%s (metrics view %s)", r.Meta.Name.Name, spec.MetricsView))
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9dbb346: the list is built from each explore's ValidSpec, so an explore without one isn't suggested, and a new runtime test covers it.

Comment thread runtime/ai/alert_report.go Outdated
if err != nil {
return err
}
if slices.ContainsFunc(slices.Concat(inst.Connectors, inst.ProjectConnectors), func(c *runtimev1.Connector) bool { return c.Name == "slack" && c.Type == "slack" }) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A slack connector with an empty bot_token passes this check. So does a project with only an unrelated connector.slack.* setting. A Slack-only alert or report then saves successfully but fails to send. Could checkSlackConfigured use Runtime.ConnectorConfig and verify the effective driver and resolved bot_token? That also follows the same connector precedence as delivery.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9dbb346: checkSlackConfigured now resolves the slack connector with Runtime.ConnectorConfig and requires the slack driver and a non-empty bot_token (a token variable that isn't set fails too), with a test for each case.

"It can send to the user and other members of the project, by email or to their Slack user, and to Slack channels, and sends to the user by email if you pass no recipients. "

// confirmBeforeDeleting is the part of the delete tools' descriptions that makes the model confirm with the user.
const confirmBeforeDeleting = "Before calling this tool, tell the user which one you will delete, by its display name, then end your turn and wait for them to confirm. "

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the PR description to match this change? It still says delete_alert returns confirmation_required and waits for a later user message. The first call now deletes the alert, and the confirmation prompt comes from the analyst's instructions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; editing the description is left to Nishant, and I've passed him the replacement lines.

…en after review

- validExplore's not-found error lists explores by their valid spec, which the create tools use, so it doesn't suggest one whose last edit failed.
- checkSlackConfigured resolves the slack connector like delivery does (Runtime.ConnectorConfig) and requires its bot token, so an empty token, an unset token variable or an unrelated connector.slack. variable no longer passes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area:AI AI assistant, chat and agents Area:Alerts Size:L Large change: 500-1,999 lines Type:Feature New feature request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants